package com.gw.vip.center.service;

import com.gw.vip.center.constant.SysArgKey;
import com.gw.vip.center.dto.IssuInfoDto;
import com.gw.vip.center.dto.OpenShopDto;
import com.gw.vip.center.jwt.TokenInfo;
import com.gw.vip.center.po.IssuInfo;
import com.gw.vip.center.po.UserInfo;
import com.gw.vip.center.service.dao_service.SysArgService;
import com.gw.vip.center.service.dao_service.IssuInfoService;
import com.gw.vip.center.service.dao_service.UserInfoService;
import com.gw.vip.center.util.HttpServletUtil;
import com.gw.vip.center.vo.IssuInfoVo;
import com.gw.vip.center.vo.OpenShopVo;
import com.gw.vip.common.constants.VipConstants;
import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.common.util.Snowflake;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * @author GW
 * @description 发行机构相关操作
 * @since 2022-04-24
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IssuService {

    private final IssuInfoService issuInfoService;
    private final Snowflake snowflake;
    private final Random random;
    private final SysArgService sysArgService;
    private final UserInfoService userInfoService;

    public OpenShopVo openShop(OpenShopDto dto) {
        TokenInfo tokenInfo = HttpServletUtil.getTokenInfo();
        IssuInfo issuInfo = issuInfoService.getByUser(tokenInfo.getUserId());
        if(issuInfo ==null){
            //发行机构为空，新建一个
            issuInfo = new IssuInfo();
            issuInfo.setId(snowflake.nextStringId());
            issuInfo.setUserId(tokenInfo.getUserId());
            issuInfo.setName(dto.getShopName());
            issuInfo.setAccessKey(String.format("%08d", random.nextInt(100000000))+String.format("%08d", random.nextInt(100000000)));
            issuInfo.setCrtTime(LocalDateTime.now());
            issuInfo.setCrtName(VipConstants.SYS);
            issuInfo.setUpdTime(issuInfo.getCrtTime());
            issuInfo.setUpdName(VipConstants.SYS);
            issuInfoService.save(issuInfo);
        }

        String url = sysArgService.getString(SysArgKey.OPEN_SHOP_GUIDE_URL);

        OpenShopVo vo = new OpenShopVo();
        vo.setIssuInfo(issuInfo);
        vo.setOpenShopGuideUrl(url);
        return vo;
    }

    /**
     * 根据用户获取发行机构信息
     * @param dto
     * @return
     */
    public IssuInfoVo issuInfo(IssuInfoDto dto) {
        UserInfo userInfo = userInfoService.getByMail(dto.getMailAddress());
        if (userInfo==null){
            log.error("用户不存在！邮箱【{}】", dto.getMailAddress());
            throw new ServiceException("用户不存在！");
        }

        //校验密码
        if (!userInfo.getPassword().equals(dto.getPassword())){
            log.error("上送密码【{}】不正确。", dto.getPassword());
            throw new ServiceException("密码错误");
        }


        IssuInfo issuInfo = issuInfoService.getByUser(userInfo.getId());
        if (issuInfo==null){
            log.error("发行机构不存在。用户【{}】", userInfo.getId());
            throw new ServiceException(ExceptionEnums.ISSU_NOT_EXIST);
        }

        IssuInfoVo vo = new IssuInfoVo();
        BeanUtils.copyProperties(issuInfo, vo);
        return vo;
    }
}
